

package com.loong.loong.hik.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loong.loong.common.core.util.R;
import com.loong.loong.common.excel.annotation.ResponseExcel;
import com.loong.loong.common.log.annotation.SysLog;
import com.loong.loong.hik.entity.HikCdcFireAlarm;
import com.loong.loong.hik.service.HikCdcFireAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 火灾报警及联动
 *
 * @author jinyiang
 * @date 2023-04-26 10:28:06
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hikcdcfirealarm" )
@Api(value = "hikcdcfirealarm", tags = "火灾报警及联动管理")
public class HikCdcFireAlarmController {

    private final  HikCdcFireAlarmService hikCdcFireAlarmService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param hikCdcFireAlarm 火灾报警及联动
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcfirealarm_view')" )
    public R getHikCdcFireAlarmPage(Page page, HikCdcFireAlarm hikCdcFireAlarm) {
		return R.ok(hikCdcFireAlarmService.page(page,
				Wrappers.query(hikCdcFireAlarm).lambda()
						.like(StringUtils.isNotEmpty(hikCdcFireAlarm.getLikeNameContent()), HikCdcFireAlarm::getContent,hikCdcFireAlarm.getLikeNameContent())
						.like(StringUtils.isNotEmpty(hikCdcFireAlarm.getLikeNameLocation()), HikCdcFireAlarm::getAlarmLocation,hikCdcFireAlarm.getLikeNameLocation())
						.like(StringUtils.isNotEmpty(hikCdcFireAlarm.getLikeAlarmName()), HikCdcFireAlarm::getAlarmName,hikCdcFireAlarm.getLikeAlarmName())
						.between(StringUtils.isNotEmpty(hikCdcFireAlarm.getStartTime())&&StringUtils.isNotEmpty(hikCdcFireAlarm.getEndTime()),HikCdcFireAlarm::getCreateTime,hikCdcFireAlarm.getStartTime(),hikCdcFireAlarm.getEndTime())
						.between(StringUtils.isNotEmpty(hikCdcFireAlarm.getStartTime2())&&StringUtils.isNotEmpty(hikCdcFireAlarm.getEndTime2()),HikCdcFireAlarm::getAlarmTime,hikCdcFireAlarm.getStartTime2(),hikCdcFireAlarm.getEndTime2()))
		);
    }


    /**
     * 通过id查询火灾报警及联动
     * @param  id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcfirealarm_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(hikCdcFireAlarmService.getById(id));
    }

    /**
     * 新增火灾报警及联动
     * @param hikCdcFireAlarm 火灾报警及联动
     * @return R
     */
    @ApiOperation(value = "新增火灾报警及联动", notes = "新增火灾报警及联动")
    @SysLog("新增火灾报警及联动" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('hik_hikcdcfirealarm_add')" )
    public R save(@RequestBody HikCdcFireAlarm hikCdcFireAlarm) {
		HikCdcFireAlarm hikCdcFireAlarm1 = new HikCdcFireAlarm();
		hikCdcFireAlarm1.setIndexCode(hikCdcFireAlarm.getIndexCode());
		long count = hikCdcFireAlarmService.count(Wrappers.query(hikCdcFireAlarm1));
		if (count>0){
			return R.failed("已存在相同的编号");
		}
        return R.ok(hikCdcFireAlarmService.save(hikCdcFireAlarm));
    }

    /**
     * 修改火灾报警及联动
     * @param hikCdcFireAlarm 火灾报警及联动
     * @return R
     */
    @ApiOperation(value = "修改火灾报警及联动", notes = "修改火灾报警及联动")
    @SysLog("修改火灾报警及联动" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('hik_hikcdcfirealarm_edit')" )
    public R updateById(@RequestBody HikCdcFireAlarm hikCdcFireAlarm) {

		HikCdcFireAlarm hikCdcFireAlarm1 = new HikCdcFireAlarm();
		hikCdcFireAlarm1.setId(hikCdcFireAlarm.getId());
		HikCdcFireAlarm one = hikCdcFireAlarmService.getOne(Wrappers.query(hikCdcFireAlarm1));
		if (!one.getIndexCode().equals(hikCdcFireAlarm.getIndexCode())){
			HikCdcFireAlarm hikCdcFireAlarm2 = new HikCdcFireAlarm();
			hikCdcFireAlarm2.setIndexCode(hikCdcFireAlarm.getIndexCode());
			long count = hikCdcFireAlarmService.count(Wrappers.query(hikCdcFireAlarm2));
			if (count>0){
				return R.failed("已存在相同的编号");
			}
		}
        return R.ok(hikCdcFireAlarmService.updateById(hikCdcFireAlarm));
    }

    /**
     * 通过id删除火灾报警及联动
     * @param  id
     * @return R
     */
    @ApiOperation(value = "通过id删除火灾报警及联动", notes = "通过id删除火灾报警及联动")
    @SysLog("通过id删除火灾报警及联动" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('hik_hikcdcfirealarm_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(hikCdcFireAlarmService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param hikCdcFireAlarm 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('hik_hikcdcfirealarm_export')" )
    public List<HikCdcFireAlarm> export(HikCdcFireAlarm hikCdcFireAlarm) {
        return hikCdcFireAlarmService.list(Wrappers.query(hikCdcFireAlarm));
    }
}
